package com.novoda.runbuddy.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.net.Uri;
import android.util.Log;

import com.novoda.runbuddy.R;
import com.novoda.runbuddy.util.SqlScanner;

public class RunBuddyContentProvider extends ContentProvider {

	private static final String										LOGTAG				= "MyLog";
	private static final String										DATABASE_NAME		= "runBuddyDB1.db";
	private static final int										DATABASE_VERSION	= 1;

	private DatabaseHelper											mOpenHelper;

	private RunBuddyContentProviderFactory							factory				= new RunBuddyContentProviderFactory();

	private static class DatabaseHelper extends SQLiteOpenHelper {
		private Context	mContext;

		public DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
			mContext = context;
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			SqlScanner scanner = new SqlScanner(mContext.getResources().openRawResource(R.raw.db_create));
			while (scanner.hasNext()) {
				db.execSQL(scanner.next());
			}
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(LOGTAG, "Upgrading database from version " + oldVersion + " to " + newVersion
					+ ", which will destroy all old data");
			SqlScanner scanner = new SqlScanner(mContext.getResources().openRawResource(R.raw.db_drop));
			while (scanner.hasNext()) {
				db.execSQL(scanner.next());
			}
			onCreate(db);
		}
	}

	@Override
	public boolean onCreate() {
		mOpenHelper = new DatabaseHelper(getContext());
		
		SQLiteDatabase mDB = getContext().openOrCreateDatabase(JogsDBSQL.DATABASE_NAME, Context.MODE_PRIVATE, null);
        try {
                mDB.execSQL(JogsDBSQL.CREATE_JOG_TABLE);
                mDB.execSQL(JogsDBSQL.CREATE_ROUTE_TABLE);
                mDB.execSQL(JogsDBSQL.CREATE_ROUTE_POINT_TABLE);
                mDB.execSQL(JogsDBSQL.CREATE_TIME_TABLE);
        }
        catch (SQLiteException e){
                //THis just means tables were already created.
        }

		
		return true;
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		return factory.getContentProvider(uri, this, mOpenHelper).delete(uri, selection, selectionArgs);
	}

	@Override
	public String getType(Uri uri) {
		return factory.getContentProvider(uri, this, mOpenHelper).getType(uri);
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		return factory.getContentProvider(uri, this, mOpenHelper).insert(uri, values);
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		return factory.getContentProvider(uri, this, mOpenHelper).query(uri, projection, selection, selectionArgs, sortOrder);
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
		return factory.getContentProvider(uri, this, mOpenHelper).update(uri, values, selection, selectionArgs);
	}

}